package com.hm.hall.commons.util;

public class ProtobufUtil {

	/*static Logger log = Logger.getLogger(ProtobufUtil.class);

	public static MsgDeliverRespProto.MsgDeliverResp coverToProto(MsgDeliverResp src) {
		Object msgBody = src.getMsgBody();
		src.setMsgBody(null);
		MsgDeliverRespProto.MsgDeliverResp.Builder builder = MsgDeliverRespProto.MsgDeliverResp.newBuilder();
		try {
			JsonFormat.parser().merge(JSON.toJSONString(src), builder);
		} catch (InvalidProtocolBufferException e) {
			e.printStackTrace();
		}
		try {
			ProtoMsgBodyPO msgBodyPO = coverToMsgBody(msgBody, src.getMsgType());
			if (msgBodyPO != null) {
				builder.setMsgBody(msgBodyPO.getContent());
				builder.setProtoType(StringValue.newBuilder().setValue(msgBodyPO.getProtoType()).build());
			}
		} catch (InvalidProtocolBufferException e) {
			e.printStackTrace();
			log.error(src.getMsgType() + "" + e.getMessage());
		}
		src.setMsgBody(msgBody);
		return builder.build();
	}

	public static ProtoMsgBodyPO coverToMsgBody(Object src, String msgType) throws InvalidProtocolBufferException {
		if(msgType == null) {
			return null;
		}
		@SuppressWarnings("rawtypes")
		com.google.protobuf.GeneratedMessageV3.Builder builder = null;
		ProtoMsgBodyPO result = new ProtoMsgBodyPO();
		switch (MsgTypeEnum.valueOf(msgType)) {
		case WP_dealNotify:
			builder = MsgDeliverRespProto.BattleDealCardPOList.newBuilder();
			result.setProtoType("BattleDealCardPOList");
			break;
		case C_updateRoomNotify:
		case C_updateRoomUserNotify:
			builder = MsgDeliverRespProto.BattleRoomMsg.newBuilder();
			result.setProtoType("BattleRoomMsg");
			break;
		case C_updateUserInfoNotify:
			builder = MsgDeliverRespProto.BattleRoomUserMsg.newBuilder();
			result.setProtoType("BattleRoomUserMsg");
			break;
		case WP_bankerChangeNotify:
			builder = MsgDeliverRespProto.BattleBankChangeMsg.newBuilder();
			result.setProtoType("BattleBankChangeMsg");
			break;
		case WP_actionNotify:
			builder = MsgDeliverRespProto.BattleActionMsg.newBuilder();
			result.setProtoType("BattleActionMsg");
			break;
		case WP_userOptNotify:
			builder = MsgDeliverRespProto.BattleUserOptMsg.newBuilder();
			result.setProtoType("BattleUserOptMsg");
			break;
		case WP_roundChangeNotify:
			builder = MsgDeliverRespProto.BattleRoundChangeMsg.newBuilder();
			result.setProtoType("BattleRoundChangeMsg");
			break;
		case C_cleanNotify:
			break;
		case WP_addScoreStatusNotify:
			builder = MsgDeliverRespProto.BattleAddScoreStatusResp.newBuilder();
			result.setProtoType("BattleAddScoreStatusResp");
			break;
		case WP_playResultNotify:
			builder = MsgDeliverRespProto.BattleThanResp.newBuilder();
			result.setProtoType("BattleThanResp");
			break;
		case WP_prolongNotify:
			builder = MsgDeliverRespProto.BattleProlongMsg.newBuilder();
			result.setProtoType("BattleProlongMsg");
			break;
		case C_imMsgNotify:
		case C_emojiNotify:
			builder = MsgDeliverRespProto.ChatMsgPO.newBuilder();
			result.setProtoType("ChatMsgPO");
			break;
		case WP_userChooseDoubleNotify:
			builder = MsgDeliverRespProto.BattleSendDoubleOptMsg.newBuilder();
			result.setProtoType("BattleSendDoubleOptMsg");
			break;
		case WP_userCSDResultNotify:
			builder = MsgDeliverRespProto.BattleCSDResultMsg.newBuilder();
			result.setProtoType("BattleCSDResultMsg");
			break;
		case WP_openSendDoubleNotify:
			builder = MsgDeliverRespProto.AllinOpenCardMsg.newBuilder();
			result.setProtoType("AllinOpenCardMsg");
			break;
		case WP_sendDoubleThanResultNotify:
			builder = MsgDeliverRespProto.BattleDoubleThanResultMsg.newBuilder();
			result.setProtoType("BattleDoubleThanResultMsg");
			break;
		case WP_needAddScoreNotify:
//				JsonFormat.parser().merge(JSON.toJSONString(src),builder);
//				return builder.build().toByteString();
			break;
		case WP_openCardNotify:
			builder = MsgDeliverRespProto.BattleOpenCardMsg.newBuilder();
			result.setProtoType("BattleOpenCardMsg");
			break;
		case WP_openCardByAllinNotify:
			builder = MsgDeliverRespProto.AllinOpenCardMsg.newBuilder();
			result.setProtoType("AllinOpenCardMsg");
			break;
		case AM_sendGameMsg:
			builder = MsgDeliverRespProto.MessageDoorPO.newBuilder();
			result.setProtoType("MessageDoorPO");
			break;
		case C_closeRoomNotify:
			break;
		case C_playLogNotify:
			builder = MsgDeliverRespProto.RoomPlayRecordOneHand.newBuilder();
			result.setProtoType("RoomPlayRecordOneHand");
			break;
		case WP_seeComCardNotify:
			builder = MsgDeliverRespProto.LookComCardsMsg.newBuilder();
			result.setProtoType("LookComCardsMsg");
			break;
		case C_updateRoomDetail:
			builder = MsgDeliverRespProto.BattleRoomDetailResp.newBuilder();
			result.setProtoType("BattleRoomDetailResp");
			break;
		case WP_waitHandsNotify:
			builder = MsgDeliverRespProto.WaitHandResp.newBuilder();
			result.setProtoType("WaitHandResp");
			break;
		case C_checkSit:
			builder = MsgDeliverReqProto.BattleSitMsg.newBuilder();
			result.setProtoType("WaitHandResp");
			break;
		case C_imMsg:
			builder = MsgDeliverReqProto.MsgTypeMsgPO.newBuilder();
			result.setProtoType("WaitHandResp");
			break;
		case WP_openCard:
			builder = MsgDeliverReqProto.BattleOpenCardReq.newBuilder();
			result.setProtoType("WaitHandResp");
			break;
		default:
			log.error("undefind message type" + msgType);
			break;
		}
		if (builder != null) {
			JsonFormat.parser().merge(JSON.toJSONString(src), builder);
			result.setContent(builder.build().toByteString());
			return result;
		}
		return null;
	}

	public static String parseMsgBody(ByteString msgBody, String msgType) {
		if(msgType == null) {
			return null;
		}

		com.google.protobuf.GeneratedMessageV3 reqProto = null;
		
		try {
			switch (MsgTypeEnum.valueOf(msgType)) {
			case C_imMsg:
				reqProto = MsgDeliverReqProto.MsgTypeMsgPO.parseFrom(msgBody);
				break;
			case WP_raise:
				reqProto = MsgDeliverReqProto.BattleRaisePO.parseFrom(msgBody);
				break;
			case C_checkSit:
			case C_sit:
				reqProto = MsgDeliverReqProto.BattleSitMsg.parseFrom(msgBody);
				break;
			case WP_openCard:
				reqProto = MsgDeliverReqProto.BattleOpenCardReq.parseFrom(msgBody);
				break;
			case WP_autoOpt:
				reqProto = MsgDeliverReqProto.BattleAutoOptPO.parseFrom(msgBody);
				break;
			case WP_addScore:
				reqProto = MsgDeliverReqProto.BattleScorePO.parseFrom(msgBody);
				break;
			case WP_chooseDouble:
				reqProto = MsgDeliverReqProto.BattleDoubleChooseReq.parseFrom(msgBody);
				break;
			case C_emoji:
				reqProto = MsgDeliverReqProto.EmojiMsgPO.parseFrom(msgBody);
				break;
			case C_forceStand:
				reqProto = MsgDeliverReqProto.ForceStandReq.parseFrom(msgBody);
				break;
			case WP_seeComCard:
				reqProto = MsgDeliverReqProto.LookComCardsReq.parseFrom(msgBody);
				break;
			case C_joinRoom:
			case C_exitRoom:
			case PongMessage:
			case WP_allin:
			case WP_call:
			case C_stand:
			case C_HiddenGame:
			case WP_check:
			case WP_fold:
			case WP_backSeat:
			case WP_prolong:
			case C_startGame:
				break;
			default:
				log.error("undefind message type" + msgType);
				break;
			}
			if(reqProto != null) {
				 //Map<FieldDescriptor, Object> jj = reqProto.getAllFields();
				//MsgDeliverReq req = JSON.parseObject(msgReqStr, MsgDeliverReq.class);
				return JsonFormat.printer().print(reqProto);
			}
		} catch (InvalidProtocolBufferException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
*/
}
